using IM.Easy.Infrastructure.Query;
using Microsoft.AspNetCore.Mvc;
using IM.Easy.SystemServices.Dtos;
using IM.Easy.SystemServices.IServices;
using IM.Easy.Entity.System;
using IM.Easy.Core.Attributes;

namespace IM.Easy.Api.Controllers.System
{
    /// <summary>
    /// SysUserRoleController 控制器
    /// </summary>
    [Route("system/userRole")]
    public class UserRoleController : ControllerBase
    {
        private readonly IUserRoleService _service;

        /// <summary>
        /// 构造函数，注入服务
        /// </summary>
        /// <param name="service">服务接口</param>
        public UserRoleController(IUserRoleService service)
        {
            _service = service;
        }

        /// <summary>
        /// 根据ID获取SysUserRole对象
        /// </summary>
        /// <param name="id">对象的ID</param>
        /// <returns>返回指定的SysUserRole对象</returns>
        [HttpGet("{id}")]
        [Obsolete]
        public SysUserRole GetById([FromRoute] long id)
        {
            var result = _service.GetById(id);
            return result ?? throw new KeyNotFoundException($"未找到ID为 {id} 的SysUserRole对象。");
        }

        /// <summary>
        /// 批量删除SysUserRole对象
        /// </summary>
        /// <param name="ids">对象的IDS</param>
        [HttpDelete]
        [Obsolete]
        public void Delete([FromBody] long[] ids)
        {
            _service.Delete(ids);
        }

        /// <summary>
        /// 单个删除SysUserRole对象
        /// </summary>
        /// <param name="id">对象的ID</param>
        [HttpDelete("{id}")]
        [Obsolete]
        public void Delete([FromRoute] long id)
        {
            _service.Delete(id);
        }

        /// <summary>
        /// 获取用户角色
        /// </summary>
        /// <param name="userId"></param>
        [HttpGet("roles/{userId}")]
        [Api(Name = "获取用户角色", Perm = "user:role")]
        public List<SysRole> GetUserRoles([FromRoute] long userId)
        {
            var roles = _service.GetUserRoles(userId);
            return roles;
        }

        /// <summary>
        /// 保存用户角色
        /// </summary>
        [HttpPost("roles")]
        [Api(Name = "保存用户角色", Perm = "user:role:save")]
        public bool SaveUserRole([FromBody] UserRoleDto dto)
        {
            return _service.SaveUserRoles(dto);
        }
    }
}
